/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.util.HibernateUtil;
import java.util.ArrayList;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import pojo.Binhluan;
import pojo.Dienthoai;
import pojo.Thongtinsanpham;

/**
 *
 * @author vinh
 */
public class DienThoaiDAO extends AbstractDAO<Dienthoai, Integer>{

    @Override
    protected Class getPoJoclass() {       
      return Dienthoai.class;
    }
    ////////////////////// phuc vu cho trang chu //////////////////////////
    public ArrayList<Dienthoai> getDienthoaitheosoluongban() // lay 10 diện thoại có số lượng bán cao nhất
    {
        ArrayList<Dienthoai> danhsachdienthoai;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Dienthoai.class);  
        criteria.addOrder(Order.desc("soluongban"));
        criteria.setMaxResults(10);
        danhsachdienthoai =(ArrayList<Dienthoai>)criteria.list();
        session.close();
        return danhsachdienthoai;
    }
    
    //////////////////// phuc vu cho trang chu //////////////////////////
    public ArrayList<Dienthoai> getDienthoaitheosoluongxem() // lay 10 diện thoại có số lượng xem cao nhất
    {
        ArrayList<Dienthoai> danhsachdienthoai;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Dienthoai.class); 
        
        criteria.addOrder(Order.desc("soluongxem"));
        criteria.setMaxResults(10);
        danhsachdienthoai =(ArrayList<Dienthoai>)criteria.list();
        session.close();
        return danhsachdienthoai;
    }
    
    ///// phuc vu cho tim kiem ben commentRight ////////////////////
    public ArrayList<Dienthoai> getDienthoaitheogiavahang(int gia, String manhasanxuat) // lay dien thoai theo gia và hang san xuat
    {
        ArrayList<Dienthoai> danhsachdienthoai;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Dienthoai.class); 
        if(gia == 1)// 1 triệu đến 3 triệu
        {
            criteria.add(Expression.between("giaban", new Integer(1000000),new Integer(3000000)));
        }
        if(gia ==3)// 3->6
        {
            criteria.add(Expression.between("giaban", new Integer(3000000),new Integer(6000000)));
        }
        if(gia ==6)// 6->9
        {
            criteria.add(Expression.between("giaban", new Integer(6000000),new Integer(9000000)));
        }
        if(gia ==9)//9->12
        {
            criteria.add(Expression.between("giaban", new Integer(9000000),new Integer(12000000)));
        }
        if(gia ==12)// 12->15 triệu
        {
            criteria.add(Expression.between("giaban", new Integer(12000000),new Integer(15000000)));
        }
        if(gia ==15)// 15->18 triệu
        {
            criteria.add(Expression.between("giaban", new Integer(15000000),new Integer(18000000)));
        }
        if(gia ==18)// 18->21 triệu
        {
            criteria.add(Expression.between("giaban", new Integer(18000000),new Integer(21000000)));
        }
        if(gia ==21)// trên 21 triệu
        {
            criteria.add(Expression.ge("giaban", new Integer(21000000)));
        }

        criteria.add(Restrictions.eq("nhasanxuat.manhasanxuat", manhasanxuat));

        danhsachdienthoai =(ArrayList<Dienthoai>)criteria.list();
        session.close();
        return danhsachdienthoai;
    }
    
    ///// phuc vu cho tim kiem ben commentRight ////////////////////
    public ArrayList<Dienthoai> getDienthoaitheoten(String tendt) /// search dien thoai theo chuoi go vao cua user
    {
        ArrayList<Dienthoai> danhsachdienthoai;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Dienthoai.class);  
        
        criteria.add(Restrictions.ilike("tendienthoai", tendt, MatchMode.ANYWHERE));
        danhsachdienthoai =(ArrayList<Dienthoai>)criteria.list();
        session.close();
        return danhsachdienthoai;
    }
    
    ///// phuc vu cho tim kiem ben commentRight ////////////////////
    /// search dien thoai theo chuoi go vao cua user//////////////
    public ArrayList<Dienthoai> getDienthoaitheonhieutieuchi(String amthanh, String chupanh,String pin,String bonho,String hienthi,String kichthuoc) 
    {
        ArrayList<Dienthoai> danhsachdienthoai= new ArrayList<Dienthoai>();
        
        
        ArrayList<Thongtinsanpham> danhsachthongtindienthoai = new ArrayList<Thongtinsanpham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Thongtinsanpham.class);
        if(amthanh != " ")
        {
            criteria.add(Restrictions.ilike("amthanh", amthanh,MatchMode.ANYWHERE));
        }
        if(chupanh != " ")
        {
            criteria.add(Restrictions.ilike("chupanh", chupanh,MatchMode.ANYWHERE));
        }
        if(pin !=" ")
        {
            criteria.add(Restrictions.ilike("pin", pin,MatchMode.ANYWHERE));
        }
        if(bonho != " ")
        {
            criteria.add(Restrictions.ilike("bonho", bonho,MatchMode.ANYWHERE));
        }
        if(hienthi != " ")
        {
            criteria.add(Restrictions.ilike("hienthi", hienthi,MatchMode.ANYWHERE));
        }
        if(kichthuoc !=" ")
        {
            criteria.add(Restrictions.ilike("kichthuoc", kichthuoc,MatchMode.ANYWHERE));
        }
        danhsachthongtindienthoai =(ArrayList<Thongtinsanpham>)criteria.list();
        session.close();
        
        if(danhsachthongtindienthoai.size()!=0)
        {
        
        DienThoaiDAO tam= new DienThoaiDAO();
        Integer[] mang = new Integer[danhsachthongtindienthoai.size()];
        for(int i=0;i< danhsachthongtindienthoai.size();i++)
        {
            
            mang[i] = danhsachthongtindienthoai.get(i).getMadienthoai();
        }
        
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria1 = session.createCriteria(Dienthoai.class);
        criteria1.add(Restrictions.in("madienthoai", mang));
        danhsachdienthoai =(ArrayList<Dienthoai>)criteria1.list();
        session.close();
        
        return danhsachdienthoai;
        }
        else
        {
            return null;
        }
    }
    
    public ArrayList<Dienthoai> getDanhSach5DienThoaiCungLoai(int madienthoai)
    {
        DienThoaiDAO dtdao = new DienThoaiDAO();
        Dienthoai tam = dtdao.get(madienthoai);
        
        ArrayList<Dienthoai> danhsachdienthoai;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Dienthoai.class); 
        criteria.add(Restrictions.like("loaidienthoai.maloaidienthoai", tam.getLoaidienthoai().getMaloaidienthoai()));
        //criteria.add(Expression.eq("loaidienthoai.maloaidienthoai", "cc"));
        criteria.setMaxResults(5);
        danhsachdienthoai =(ArrayList<Dienthoai>)criteria.list();
        session.close();
        return danhsachdienthoai;
        
    }
    
}
